package org.lk.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.lk.reggie.common.R;
import org.lk.reggie.dto.SetmealDto;
import org.lk.reggie.entity.Setmeal;
import org.lk.reggie.service.CategoryService;
import org.lk.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController
{

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    @Cacheable(value = "setmealCachePage", key = "'page_' + #currentPage")
    public R<Page<SetmealDto>> getAll(@RequestParam("page") Integer currentPage, @RequestParam Integer pageSize, String name)
    {
        // 构造分页控制器
        Page<Setmeal> pageInfo = new Page<>(currentPage, pageSize);
        // 构造条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Setmeal :: getName, name);
        // 执行查询
        setmealService.page(pageInfo, queryWrapper);

        Page<SetmealDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");

        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = new ArrayList<>();
        for (Setmeal setmeal:
             records)
        {
            String name1 = categoryService.getById(setmeal.getCategoryId()).getName();
            SetmealDto dto = new SetmealDto();
            dto.setCategoryName(name1);
            BeanUtils.copyProperties(setmeal, dto);
            list.add(dto);
        }
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }


    @PostMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> saveSetmeal(@RequestBody SetmealDto dto)
    {
        log.info("套餐信息：{}", dto);

        setmealService.mySave(dto);

        return R.success("success");
    }


    @GetMapping("/{id}")
    public R<Setmeal> updateSetmeal(@PathVariable Long id)
    {
        Setmeal byId = setmealService.getById(id);
        return R.success(byId);
    }


    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> deleteSetmeal(@RequestParam Long ids)
    {
        setmealService.myDelete(ids);
        return R.success("success");
    }


    @PostMapping("/{ids}")
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> updateSetmeals(@PathVariable String ids)
    {
        // 删除逻辑


        return R.error("error");
    }

    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "'setmeal_' + #categoryId")
    public R<List<Setmeal>> getActiveSetmeal(@RequestParam Long categoryId, @RequestParam Integer status)
    {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal :: getCategoryId, categoryId);
        queryWrapper.eq(Setmeal :: getStatus, status);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);

    }
}
